package com.powerbank.adminsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.powerbank.adminsystem.entity.AdminRole;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 角色Mapper接口
 */
@Mapper
public interface AdminRoleMapper extends BaseMapper<AdminRole> {

    /**
     * 根据角色编码查询角色
     */
    @Select("SELECT * FROM admin_role WHERE role_code = #{roleCode} AND deleted = 0")
    AdminRole findByRoleCode(@Param("roleCode") String roleCode);

    /**
     * 根据用户ID查询角色列表
     */
    @Select("SELECT r.* FROM admin_role r " +
            "JOIN admin_user_role ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND r.deleted = 0 AND ur.deleted = 0")
    List<AdminRole> findRolesByUserId(@Param("userId") Long userId);

    /**
     * 根据角色ID列表查询角色列表
     */
    @Select("<script>" +
            "SELECT * FROM admin_role WHERE id IN " +
            "<foreach item='id' collection='roleIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "AND deleted = 0" +
            "</script>")
    List<AdminRole> findRolesByIds(@Param("roleIds") List<Long> roleIds);
}